package com.guigu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.ProductCommentMapper;
import com.guigu.mapper.ProductImgMapper;
import com.guigu.mapper.ProductMapper;
import com.guigu.mapper.ProductTypeMapper;
import com.guigu.pojo.Product;
import com.guigu.pojo.ProductComment;
import com.guigu.pojo.ProductImg;
import com.guigu.pojo.ProductType;
import com.guigu.service.ProductCommentService;
import com.guigu.service.ProductService;
import com.guigu.service.ProductTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author Mr.Zhang
 * @since 2025-03-12
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Autowired
    private ProductCommentService productCommentService;

    @Autowired
    private ProductImgMapper productImgMapper;

    @Override
    public Page<Product> getProducts(HashMap<String, Object> map) {
        Integer currentPage = 1;
        Integer pageSize = 5;
        Integer type = 0;

        Object pageNumObj = map.get("currentPage");
        if (pageNumObj != null) {
            if (pageNumObj instanceof String && StringUtils.isNotBlank((String) pageNumObj)) {
                currentPage = Integer.parseInt((String) pageNumObj);
            } else if (pageNumObj instanceof Integer) {
                currentPage = (Integer) pageNumObj;
            }
        }

        Object pageSizeObj = map.get("pageSize");
        if (pageSizeObj != null) {
            if (pageSizeObj instanceof String && StringUtils.isNotBlank((String) pageSizeObj)) {
                pageSize = Integer.parseInt((String) pageSizeObj);
            } else if (pageSizeObj instanceof Integer) {
                pageSize = (Integer) pageSizeObj;
            }
        }
        Object typeObj = map.get("type");
        if (typeObj != null) {
            if (typeObj instanceof String && StringUtils.isNotBlank((String) typeObj)) {
                type = Integer.parseInt((String) typeObj);
            } else if (typeObj instanceof Integer) {
                type = (Integer) typeObj;
            }
        }


        Page<Product> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if (type != 0) {
            QueryWrapper<ProductType> TypeQueryWrapper = new QueryWrapper<>();
            TypeQueryWrapper.eq("type_id", type);
            ProductType productType = productTypeMapper.selectOne(TypeQueryWrapper);

            queryWrapper.eq("type_id", productType.getParentId()+"-"+productType.getTypeId());
        }
        if (map.get("keywordInput") != null && StringUtils.isNotBlank((String) map.get("keywordInput"))) {
            queryWrapper.like("name",map.get("keywordInput"));
        }
        Page<Product> productPage = productMapper.selectPage(page, queryWrapper);
        return productPage;
    }

    @Override
    public Product getProductById(Integer id) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",id);
        if (productMapper.selectOne(queryWrapper)!=null){
            return productMapper.selectOne(queryWrapper);
        }
        return null;
    }

    @Override
    public List<Product> getSellingProduct() {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("on_off",1);
        queryWrapper.orderByDesc("total_number_of_comments");
        queryWrapper.last("limit 10");
        if (productMapper.selectList(queryWrapper)!=null){
            List<Product> products = productMapper.selectList(queryWrapper);
            products = completeTheProductInformation(products);
            return products;
        }
        return null;
    }

    private List<Product> completeTheProductInformation(List<Product> products) {
        for (Product product : products) {
            Integer productId = product.getProductId();
            List<ProductComment> productCommentById = productCommentService.getProductCommentById(productId);
            product.setProductComments(productCommentById);

            QueryWrapper<ProductImg> productImgQueryWrapper = new QueryWrapper<>();
            productImgQueryWrapper.eq("product_id",productId);
            if (productImgMapper.selectList(productImgQueryWrapper)!=null){
                List<ProductImg> productImgs = productImgMapper.selectList(productImgQueryWrapper);
                product.setProductImgs(productImgs);
            }
        }
        return products;
    }

    @Override
    public Page<Product> getProductBySearchData(HashMap<String, Object> map) {
        Integer currentPage = 1;
        Integer pageSize = 5;
        Object pageNumObj = map.get("currentPage");
        if (pageNumObj != null) {
            if (pageNumObj instanceof String && StringUtils.isNotBlank((String) pageNumObj)) {
                currentPage = Integer.parseInt((String) pageNumObj);
            } else if (pageNumObj instanceof Integer) {
                currentPage = (Integer) pageNumObj;
            }
        }

        Object pageSizeObj = map.get("pageSize");
        if (pageSizeObj != null) {
            if (pageSizeObj instanceof String && StringUtils.isNotBlank((String) pageSizeObj)) {
                pageSize = Integer.parseInt((String) pageSizeObj);
            } else if (pageSizeObj instanceof Integer) {
                pageSize = (Integer) pageSizeObj;
            }
        }
        Page<Product> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if (map.get("keyword") != null && StringUtils.isNotBlank((String) map.get("keyword"))) {
            queryWrapper.like("name",map.get("keyword"));
        }
        if (map.get("typeId") != null && StringUtils.isNotBlank((String) map.get("typeId"))) {
            queryWrapper.eq("type_id",map.get("typeId"));
        }
        if (map.get("price") != null && StringUtils.isNotBlank((String) map.get("price"))) {
            String[] split = ((String) map.get("price")).split("-");
            queryWrapper.between("price",split[0],split[1]);
        }
        Page<Product> productPage = productMapper.selectPage(page, queryWrapper);
        productPage.setRecords(completeTheProductInformation(productPage.getRecords()));

        return productPage;
    }
    @Override
    public Page<Product> getProduct(HashMap<String, Object> map) {
        Integer currentPage = 1;
        Integer pageSize = 5;

        Object pageNumObj = map.get("currentPage");
        if (pageNumObj != null) {
            if (pageNumObj instanceof String && StringUtils.isNotBlank((String) pageNumObj)) {
                currentPage = Integer.parseInt((String) pageNumObj);
            } else if (pageNumObj instanceof Integer) {
                currentPage = (Integer) pageNumObj;
            }
        }
        Object pageSizeObj = map.get("pageSize");
        if (pageSizeObj != null) {
            if (pageSizeObj instanceof String && StringUtils.isNotBlank((String) pageSizeObj)) {
                pageSize = Integer.parseInt((String) pageSizeObj);
            } else if (pageSizeObj instanceof Integer) {
                pageSize = (Integer) pageSizeObj;
            }
        }

        Page<Product> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if (map.get("productName") != null && StringUtils.isNotBlank((String) map.get("productName"))) {
            queryWrapper.like("name", map.get("productName"));
        }
        if (map.get("status") != null && StringUtils.isNotBlank((String) map.get("status"))) {
            queryWrapper.like("on_off",map.get("status"));
        }
        if (map.get("firstCategory") != null && StringUtils.isNotBlank((String) map.get("firstCategory"))) {
            queryWrapper.like("type_id",map.get("firstCategory"));
        }
        if (map.get("secondCategory") != null && StringUtils.isNotBlank((String) map.get("secondCategory"))) {
            queryWrapper.like("type_id",map.get("secondCategory"));
        }

        Page<Product> productPage = productMapper.selectPage(page, queryWrapper);
        return productPage;
    }
}
